/*
 * animation.cpp
 *
 *  Created on: 11.09.2010
 *      Author: 123
 */
#include "animation.h"
#include <oslib/oslib.h>
#include <stdlib.h>
#include "Timer.h"
#include "Colors.h"
//extern pgeFont *nash;
double states[16][16][3] = { { 85, 82, -79, 105, 59, 34, 69, 84, 31, 91, 56,
		93, 74, 68, -34, 72, 61, -32, 112, 68, -90, 72, 75, 68, 87, 66, 68 }, {
		92, 85, -59, 111, 62, 46, 75, 89, 34, 93, 59, 82, 74, 70, -29, 72, 59,
		-30, 115, 69, -90, 79, 76, 66, 68, 50, 72 }

};
double states_param[16][3] = { { 0.1, 0.06, 1 }, { 0.1, 0.6, 0 }, { 0.1, 0.06,
		0 } };

OSL_IMAGE* parts[2][16];
void Animation::Draw(int x, int y) {
	double tx = 0;
	double ty = 0;
	double ta = 0;
	double nx = 0;
	double ny = 0;
	double na = 0;

	double at = 0;
	double t = 0;
	int i = 0;
	double sa = 0;
	//oslDrawStringf(100,100,"%f",timer->getTimer());
	//AnimateChar->StartDrawing();
	if ((timer->getTimer() < states_param[CurrentState][Time])
	//	|| (!this->animate)) {
	) {
		for (i = width - 1; i > -1; i--) {

			//	if(i==5){

			//	}
			//	else{
			///	if(ang>360)
			//		ang=0;
			//	if(ang<0)
			//		ang=360;
			double SinAngle = sin((double) ang * 3.14 / 180);
			double CosAngle = cos((double) ang * 3.14 / 180);
			//fprintf(stderr,"ang=%f,%f",SinAngle,CosAngle);
			//	else{
			//					if(ang==-1)
			//						ang=360;
			//					int SinAngle=sin(ang*3.14/180);
			//					int CosAngle=cos(ang*3.14/180);

			//tx=(states[CurrentState][i][0])*cos(ang*3.14/180)+(states[CurrentState][i][1])*sin(ang*3.14/180);
			//ty=(states[CurrentState][i][0])*sin(ang*3.14/180)+(states[CurrentState][i][1])*cos(ang*3.14/180);
			//	}
			//	tx=x+(states[CurrentState][i][0]-x)*CosAngle+(states[CurrentState][i][1]-y)*SinAngle;
			//	ty=y+(states[CurrentState][i][0]-x)*SinAngle+(states[CurrentState][i][1]-y)*CosAngle;
			//	}
			//tx=states[CurrentState][i][0]*CosAngle;
			//ty=states[CurrentState][i][1]*SinAngle;
			tx = (states[CurrentState][i][0]) * CosAngle -
					(states[CurrentState][i][1]) * SinAngle;
			ty = (states[CurrentState][i][0]) * SinAngle +
					(states[CurrentState][i][1]) * CosAngle;
			//	}(states[CurrentState][i][0])*CosAngle-(states[CurrentState][i][0])*SinAngle;


			parts[AnimNum[i]][i]->angle = ang - states[CurrentState][i][2];
			oslDrawImageXY(parts[AnimNum[i]][i], 6 +parts[0][i]->stretchX/2 +
					x + tx , // x
					ty+parts[0][i]->stretchY/2 + y // y


			);

		}
	} else {
		for (i = width - 1; i > -1; i--) {
			tx = states[CurrentState][i][0];
			ty = states[CurrentState][i][1];
			ta = -states[CurrentState][i][2];
			nx = states[(int) states_param[CurrentState][2]][i][0];
			ny = states[(int) states_param[CurrentState][2]][i][1];
			na = states[(int) states_param[CurrentState][2]][i][2];
			t = states_param[CurrentState][Time];
			at = states_param[CurrentState][aTime];
			//	pgeFontActivate(nash);
			//	pgeFontPrintf(nash,400,80,BLACK,"%d",nx );
			//pgeFontPrintf(nash,360,20,BLACK,"%d",def->animation.ang);
			//	pgeFontPrintf(nash,380,60,BLACK,"%d", ny);
			//parts[i]->angle= ta+(na-ta)/at*(timer->getTimer()-t);
			sa = ((timer->getTimer() - t)) ? at * (timer->getTimer() - t) : 1;
			parts[AnimNum[i]][i]->angle = (int) (ta + (na - ta) / sa);
			//	parts[AnimNum[i]][i]->angle=na;
			//	oslDrawImageXY(parts[AnimNum[i]][i], nx, // x
			//	ny// y
			//	);
			oslDrawImageXY(parts[AnimNum[i]][i], (int)(x - 6 + tx + (nx - tx)
							/sa), // x
					(int)(y + ty + (ny - ty) /
							sa) // y
					//
			);
			//fprintf(stderr," %f ",ta + (na - ta) /(at * (timer->getTimer() - t)));
			fprintf(stderr, "%d,%f %f ", i,
					tx + (nx - tx) / at * (timer->getTimer() - t),
					y + ty + (ny - ty) / (at * (timer->getTimer() - t)));
			fprintf(stderr, "%d,%f %f %f %f %f", i, ty, (ny - ty), at,
					(timer->getTimer() - t),
					(ny - ty) / at * (timer->getTimer() - t));

		}
	}
	//AnimateChar->EndDrawing();
	//AnimateChar->SwapBuffers(NO_WAIT_VSYNC);


}

void Animation::Load(const char* name) {
	char line[32];
	char* str = line;

	FILE * fd = fopen(name, "r");
	if (!fd)
		fprintf(stderr, "fd load fail");
	int i = 0;

	float c;
	int j;
	int k;
	while (1) {
		char TexName[32] = "data/";
		if (!fgets(line, 1024, fd))
			break;
		if (line[0] == '*')
			break;
		sscanf(line, "%s", TexName + 5);
		sprintf(LoadingInfo, "%d,%s", i + 1, TexName);
		parts[0][i] = oslLoadImageFile(TexName, OSL_IN_RAM, OSL_PF_8888);
		oslAssert(parts[0][i]);
		parts[0][i]->centerX = parts[0][i]->stretchX / 2;
		parts[0][i]->centerY = parts[0][i]->stretchY / 2;
		fprintf(stderr, "%d,%s", i + 1, TexName);

		i++;
	}
	width = i;

	i = 0;

	while (1) {

		fscanf(fd, "%f", &c);
		if (c == -1)
			break;
		states_param[i][0] = c / 10;
		fscanf(fd, "%f", &c);
		states_param[i][1] = c / 10;
		fscanf(fd, "%f", &c);
		states_param[i][2] = (int) c;
		for (j = 0; j < width; j++) {

			for (k = 0; k < 3; k++) {
				fscanf(fd, "%f", &c);
				states[i][j][k] = c;
			}
		}
		i++;

	}
	CenterXY();
	fclose(fd);

}
void Animation::CenterXY() {
	//float X,Y;
	for (int j = 0; j < 4; j++) {
		//	X=states[j][5][0];
		//	Y=states[j][5][1];
		for (int i = 0; i < width; i++) {
			states[j][i][0] -= 77;
			states[j][i][1] -= 77;
			fprintf(stderr, "%f %f %f", states[j][i][0], states[j][i][1],
					states[j][i][2]);

		}
	}
}

